/* 
 * File:   backbookindex.h
 * Author: Luis Aguirre
 * Description: Represents the back book index of an ebook
 *
 * Created on jule 17, 2013, 06:30 PM
 */

#ifndef BACKBOOKINDEX_H
#define	BACKBOOKINDEX_H

#include <string>
#include <string.h>
#include <set>
#include <vector>
#include "../exceptions/fileexception.h"
#include "../exceptions/filenotfoundexception.h"
#include "../exceptions/nullpathexception.h"
#include "../exceptions/openfileexception.h"

/**
 * Word that contains the occurrences where the word apears in the ebook.
 */
struct Word
{   
    /**
     * Constructor that initialize the data.
     * 
     * @param word Word
     */
    Word(std::string word) : data(word)
    {
    }
    
    /**
     * Constructor copy.
     * 
     * @param other Another word
     */
    Word(const Word& other)
    {
        data = other.data;
        occurrences = other.occurrences;
    }
    
    bool operator<(const Word& other) const
    {
        return strcasecmp(data.c_str(), other.data.c_str()) < 0;
    }
    
    /**
     * Word
     */
    std::string data;
    
    /**
     * Occurrences where the word apears.
     */
    std::set<int> occurrences;
};

struct BackBookIndex
{
    /**
     * Constructor
     */
    BackBookIndex()
    {
    }
    
    /**
     * Constructor copy.
     *  
     * @param other Another BackBookIndex
     */
    BackBookIndex(const BackBookIndex& other);
    
    /**
     * Destructor just clear the words.
     */
    ~BackBookIndex()
    {
        words.clear();
    }
    
    /**
     * Write the back book index in the output target
     * 
     * @param ouput Output target
     */
    void write(const char* output) throw (FileException);
    
    /**
     * Add a word if the word was added increment the occurrence
     * 
     * @param word Word that will be added
     * @param page Number page
     * 
     * @return True if the word wass added
     */
    bool addWord(Word word, int page);
    
    std::vector<Word> words;
};

#endif	/* BACKBOOKINDEX_H */

